package com.ruoyi.classroom.course.service.impl;

import com.ruoyi.classroom.course.domain.MyTeacherCourse;
import com.ruoyi.classroom.course.domain.StuCourseVo;
import com.ruoyi.classroom.course.domain.StudentCourse;
import com.ruoyi.classroom.course.mapper.StudentCourseMapper;
import com.ruoyi.classroom.course.service.IStudentCourseService;
import com.ruoyi.classroom.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 我要选课Service业务层处理
 * 
 * @author lgm
 * @date 2024-07-20
 */
@Service
public class StudentCourseServiceImpl implements IStudentCourseService
{
    @Autowired
    private StudentCourseMapper studentCourseMapper;

    /**
     * 查询我要选课
     * 
     * @param id 我要选课主键
     * @return 我要选课
     */
    @Override
    public StudentCourse selectStudentCourseById(Long id)
    {
        return studentCourseMapper.selectStudentCourseById(id);
    }

    /**
     * 查询我要选课列表
     * 
     * @param studentCourse 我要选课
     * @return 我要选课
     */
    @Override
    public List<StudentCourse> selectStudentCourseList(StudentCourse studentCourse)
    {
        List<StudentCourse> studentCoursesAll = studentCourseMapper.selectStudentCourseList(studentCourse);
        // 使用流式操作一次性筛选出符合条件的课程，并使用LinkedHashMap去重
        Map<String, StudentCourse> filteredMap = studentCoursesAll.stream()
                .filter(c -> "第二学期".equals(c.getTerm())
                        && "2024-2025".equals(c.getYears())
                        && c.getIsUsed() == 0)
                .collect(Collectors.toMap(
                        StudentCourse::getName,
                        c -> c,
                        (oldVal, newVal) -> oldVal,
                        LinkedHashMap::new));
        // 将Map转换回List
        ArrayList<StudentCourse> studentCourses = new ArrayList<>(filteredMap.values());

        for (StudentCourse studentCours : studentCourses) {
            Long calcNowNum = studentCourseMapper.calcNowNum(studentCours.getId());
            studentCours.setNownum(calcNowNum);
            studentCourseMapper.updateStudentCourse(studentCours);
        }
        return studentCourses;
    }

    /**
     * 新增我要选课
     * 
     * @param studentCourse 我要选课
     * @return 结果
     */
    @Override
    public int insertStudentCourse(StudentCourse studentCourse)
    {
        return studentCourseMapper.insertStudentCourse(studentCourse);
    }

    /**
     * 修改我要选课【选课操作】
     * 
     * @param studentCourse 我要选课
     * @return 结果
     */
    @Override
    public int updateStudentCourse(StudentCourse studentCourse)
    {
        Long nownum = studentCourse.getNownum();
//      和数据库获取的当前人数对比，相等就不让选课,返回0
        if (nownum .equals(studentCourse.getMaxnum())
                || nownum > studentCourse.getMaxnum() ) {
            return 0;
        }
//        如果已经有课表对应的学生选课信息，就返回-1，不让选课
        List<StuCourseVo> stuCourseVos = studentCourseMapper.selectCourseIdByUserId(studentCourse.getId());
        for (StuCourseVo vo : stuCourseVos) {
            if(vo.getUserId().equals(studentCourse.getUserId())){
                return -1;
            }
        }
        studentCourse.setNownum(nownum+1);
        boolean result = studentCourseMapper.insertNowNum(studentCourse) > 0;
        if (result) {
        return studentCourseMapper.updateStudentCourse(studentCourse);
        }else {
            return 0;
        }
    }

    /**
     * 修改我要选课【退课操作】
     *
     * @param studentCourse 我要选课
     * @return 结果
     */
   /* @Override
    public int deupdateStudentCourse(StudentCourse studentCourse)
    {
        int nownumResult =0;
//         id,user_id,course_id  from student_course
        List<StuCourseVo> courseVos = studentCourseMapper.selectCourseIdByUserId(studentCourse.getId());
        for (StuCourseVo vo : courseVos) {
            if(vo.getUserId().equals(studentCourse.getUserId())){
                studentCourse.setNownum(studentCourseMapper.selectStudentCourseById(studentCourse.getId()).getNownum()-1);
//                id,user_id,course_id ， Nownum
                nownumResult = studentCourseMapper.deupdateStudentCourse(studentCourse);
            }
        }
//        删除掉student_course表中存入的学生和课表对应的信息
        int deleteNowNum = studentCourseMapper.deleteNowNum(studentCourse);
        if (deleteNowNum > 0 && nownumResult > 0){
            return 1;
        }else {
            return 0;
        }
    }*/
    @Override
    public int deupdateStudentCourse(StudentCourse studentCourse)
    {
        int nownumResult = 0;
        // id,user_id,course_id  from student_course
        List<StuCourseVo> courseVos = studentCourseMapper.selectCourseIdByUserId(studentCourse.getId());
        for (StuCourseVo vo : courseVos) {
            if(vo.getUserId().equals(studentCourse.getUserId())) {
                StudentCourse existingCourse = studentCourseMapper.selectStudentCourseById(studentCourse.getId());
                if (existingCourse != null) { // 防止潜在的NullPointerException
                    studentCourse.setNownum(existingCourse.getNownum() - 1); // 假设setNowNum是正确的方法名
//                    修改数据库
                    existingCourse.setNownum(studentCourse.getNownum());
                    studentCourseMapper.updateStudentCourse(existingCourse);
                    nownumResult = studentCourseMapper.deupdateStudentCourse(studentCourse);
                }
            }
        }
        // 删除掉student_course表中存入的学生和课表对应的信息
        int deleteNowNum = studentCourseMapper.deleteNowNum(studentCourse);
        if (deleteNowNum > 0 && nownumResult > 0) {
            return 1;
        } else {
            return 0;
        }
    }


    /**
     * 批量删除我要选课
     * 
     * @param ids 需要删除的我要选课主键
     * @return 结果
     */
    @Override
    public int deleteStudentCourseByIds(Long[] ids)
    {
        return studentCourseMapper.deleteStudentCourseByIds(ids);
    }

    /**
     * 删除我要选课信息
     * 
     * @param id 我要选课主键
     * @return 结果
     */
    @Override
    public int deleteStudentCourseById(Long id)
    {
        return studentCourseMapper.deleteStudentCourseById(id);
    }
    @Override
    public List<User> selectTeacherList() {
        return studentCourseMapper.selectTeacherList();
    }
}
